﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.BLL;
using Com.CMS263.PO;
using Com.CMS263.Util;
using System.IO;

namespace Com.CMS263.Web.manage.SubBaseArt
{
    public partial class ModArt : System.Web.UI.Page
    {
        public ExtAttributeBLL extbll = new ExtAttributeBLL();
        public IList<ExtAttribute> extList = null;
        public ArtTypeBLL atbll = new ArtTypeBLL();
        public int page = 1;
        public int pid;
       
        string key;
        public ArtBaseBLL abll = new ArtBaseBLL();
        public ArtBase ab = new ArtBase();
        public ExtValueBLL evbll = new ExtValueBLL();
        public ExtValue extvalue = new ExtValue();
        public Com.CMS263.PO.AttributeType type = null;
        public int id = 0;
        int menuId;
        public int maxOrder = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            pid = Convert.ToInt32(Request.QueryString.Get("pid"));
            menuId = int.Parse(Request["artid"]);
            key = Request["key"];
            extList = extbll.GetExtAttributeByTypeId(pid);
            id = int.Parse(Request["id"]);
            type = (new AttributeTypeBLL()).GetAttributeById(pid);
            page = string.IsNullOrEmpty(Request["page"]) ? 1 : int.Parse(Request["page"]);
            this.lbTypeName.Text = type.typeName;
            this.hdfTypeId.Value = ab.typeId.ToString();
            if (!Page.IsPostBack)
            {
                //根据父类类型盘点是否出默认结果
               
                if (id != 0)
                {

                    ab = abll.GetArtBaseById(id);
                    string tit = ab.title;
                    if (tit.IndexOf("<b>") > 0)
                    {
                        this.chkStrong.Checked = true;


                    }
                    if (tit.IndexOf("color:red") > 0)
                    {
                        this.chkRed.Checked = true;
                    }
                    maxOrder = ab.orderNum;
                    if (ab.isBest == 0)
                    {
                        chkBest.Checked = false;
                    }
                    else
                    {
                        chkBest.Checked = true;
                    }


                }
                else
                {
                    Response.Redirect("BaseArtList.aspx");
                }
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ArtBaseBLL abbll = new ArtBaseBLL();
            //文件按天存储，没有目录就自己创建
            string strDay = DateTime.Now.ToString("yyyy-MM-dd");
            string savePath = Server.MapPath("/ArtUpFile/" + strDay);
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }

            
            string addDate = Request["addDate"];
           
           
            id = Convert.ToInt32(Request.QueryString.Get("id"));
            ab = abbll.GetArtBaseById(id);
            
            ab.addDate = DateTime.Parse(addDate);
            ab.clickCount = int.Parse(Request["clickCount"]);

            string ordernums = Request["OrderNum"];
            int ordernum = 1;
            try
            {
                ordernum = Convert.ToInt32(ordernums);
                if (ordernum <= 0)
                {
                    StringUtil.Alert("排序请输入正整数", "-1");
                }
            }
            catch
            {

                StringUtil.Alert("排序请输入正整数", "-1");
            }

            ab.isBest = chkBest.Checked == true ? 1 : 0;
            ab.isShow = 1;
            ab.orderNum = ordernum;
           
            string title = Request["title"];
            if (chkStrong.Checked)
            {
                title = "<b>" + title + "</b>";

            }
            if (this.chkRed.Checked)
            {
                title = "<span style='color:red'>" + title + "</span>";
            }
            ab.title = title;
            IList<ExtAttribute> attrList = extbll.GetExtAttributeByTypeId(pid);
            IList<ExtValue> valList = new List<ExtValue>();
             int i = 0;
            //循环属性列表得到属性值并天数值列表
            foreach (ExtAttribute item in attrList)
            {
                string val = "";
                int notNull = item.notNull;
                if (item.attrType == 4 || item.attrType == 5)
                {
                    HttpPostedFile hpf = Request.Files[item.attrName];
                    if (hpf.ContentLength > 5)
                    {
                        val = strDay + "/" + UpLoadFileUtil.UpLoadFile(Request.Files[item.attrName], savePath, i.ToString(), item.attrValue);
                    }
                }
                else if (item.attrType == 2)
                {
                    val = Request[item.attrName].Replace("'", "’");
                }
                else
                {
                    if (notNull == 1)
                    {
                        if (string.IsNullOrEmpty(Request[item.attrName]))
                        {
                            StringUtil.Alert(item.attrTxtName + "是必填的", "-1");
                            return;
                        }
                    }
                    val = Request[item.attrName];
                } i++;

                ExtValue ev = evbll.GetExtValueById(id,item.id);

                if (ev != null)
                {
                    foreach (ExtAttribute exa in extList)
                    {

                        if (exa.id == ev.attrId)
                        {
                            if (!string.IsNullOrEmpty(val))
                            {
                                ev.val = val;
                            }
                        }
                    }
                    valList.Add(ev);
                }
                else 
                {
                    ExtValue evv = new ExtValue();
                    evv.baseArtId = id;
                    evv.attrId = item.id;
                    evv.val = "";
                    evbll.Add(evv);
                }

            }
            ab.valueList = valList;

            abbll.UpdateArt(ab);

            AdminLoginInfo ali = AdminLoginCookie.GetLoginInfo();
            ArtTypeBLL atbll = new ArtTypeBLL();
            OperationLogBLL olbll = new OperationLogBLL();
            OperationLog ol = new OperationLog();
            ol.AddDate = DateTime.Now;
            ol.Ip = StringUtil.GetIP();
            ol.ModuleName = (new AttributeTypeBLL()).GetAttributeById(pid).typeName;
            ol.Contents = "【修改】" + ab.title;
            ol.UserName = ali.UserName;
            olbll.Add(ol);
            StringUtil.Alert("修改成功", "BaseArtList.aspx?pid=" + ab.typeId + "&artid=" + ab.artId + "&page=" + page + "&key=" + key);

        }

        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {

        }
        public string GetText(object obj)
        {
            return obj.ToString().Replace("<span style='color:red'>","").Replace("</span>","").Replace("<b>","").Replace("</b>","");
        }
    }
}
